import {Component, Output} from '@angular/core';
import {NavController, ViewController, PopoverController, ModalController} from 'ionic-angular';
import {EmojiPopoverPage} from "../emoji-popover/emoji-popover";
import {EditorService} from "../../services/EditorService";
import {Camera, Keyboard,} from 'ionic-native';

/*
 Generated class for the AddStory page.

 See http://ionicframework.com/docs/v2/components/#navigation for more info on
 Ionic pages and navigation.
 */
@Component({
  selector: 'page-add-story',
  templateUrl: 'add-content.html',
  providers: [EditorService]
})
export class AddContentPage {

  disableEmoji: boolean = false;
  disablePicture: boolean = false;
  disableSubject: boolean = false;

  paddingBottom: number = 32;

  disablePictureClick: boolean = false;
  title: string = 'Add Content';
  maxImgCount: number = 9;

  constructor(public navCtrl: NavController,
              public viewCtrl: ViewController,
              public modalCtrl: ModalController,
              public popoverCtrl: PopoverController,
              public editorService: EditorService) {
    if (viewCtrl.data.disableEmoji) {
      this.disableEmoji = true;
    }
    if (viewCtrl.data.disablePicture) {
      this.disablePicture = true;
    }
    if (viewCtrl.data.disableSubject) {
      this.disableSubject = true;
    }
    if (viewCtrl.data.title) {
      this.title = viewCtrl.data.title;
    }
    editorService.getSource('add-content-emojione').subscribe(emoji_id => {
      this.currentText += `[emoji-${emoji_id}]`;
    });
    Keyboard.onKeyboardHide().subscribe(e => {
      this.paddingBottom = 0;
    });
    Keyboard.onKeyboardShow().subscribe(e => {
      this.paddingBottom = 32;
    })
  }

  @Output()
  currentText: string = '';

  imgUrls: Array<string> = [];

  dismiss() {
    this.viewCtrl.dismiss();
  }

  popupEmoji(e: any): void {
    this.popoverCtrl.create(EmojiPopoverPage, {emojiInputName: 'add-content-emojione'}).present({ev: e});
  }

  showGallery() {
    Camera.getPicture({
      quality: 100,
      allowEdit: true,
      sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
      mediaType: Camera.MediaType.PICTURE,
      saveToPhotoAlbum: false,
      destinationType: Camera.DestinationType.FILE_URI
    }).then((imageData) => {
      // imageData is either a base64 encoded string or a file URI
      // If it's base64:
      this.imgUrls.push(imageData);
      if (this.maxImgCount == this.imgUrls.length) {
        this.disablePictureClick = true;
      }
    }, (err) => {
      // Handle error
    });
  }

  showCamera() {
    Camera.getPicture({
      quality: 100,
      allowEdit: true,
      mediaType: Camera.MediaType.PICTURE,
      saveToPhotoAlbum: true,
      destinationType: Camera.DestinationType.FILE_URI
    }).then((imageData) => {
      // imageData is either a base64 encoded string or a file URI
      this.imgUrls.push(imageData);
      if (this.maxImgCount == this.imgUrls.length) {
        this.disablePictureClick = true;
      }
      // If it's base64:
    }, (err) => {
      // Handle error
    });
  }
}
